package com.cy.utils;

import jodd.io.FileUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 纯Java文件操作工具，支持文件、文件夹的复制、删除、移动操作。
 */
public class FileToolkit {

    public static String NEWPATH = PropertyUtil.getGrosseValue(PropertyUtil.UPLOAD, "excelDir").trim();
    public static String OLDPATH = PropertyUtil.getGrosseValue(PropertyUtil.UPLOAD, "tempDir").trim();
    public static String PICPATH = PropertyUtil.getGrosseValue(PropertyUtil.UPLOAD, "picDir").trim();

    public FileToolkit() {
    }

    /**
     * 删除文件（夹）
     *
     * @param file 文件（夹）
     */
    public static void delete(File file) {
        if (!file.exists()) return;
        if (file.isFile()) {
            file.delete();
        } else {
            for (File f : file.listFiles()) {
                delete(f);
            }
            file.delete();
        }
    }

    /**
     * 复制文件（夹）到一个目标文件夹
     *
     * @param resFile       源文件（夹）
     * @param objFolderFile 目标文件夹
     * @throws java.io.IOException 异常时抛出
     */
    public static void copy(File resFile, File objFolderFile) throws IOException {
        if (!resFile.exists()) return;
        if (!objFolderFile.exists()) objFolderFile.mkdirs();
        if (resFile.isFile()) {
            File objFile = new File(objFolderFile.getPath() + File.separator + resFile.getName());
            //复制文件到目标地
            InputStream ins = new FileInputStream(resFile);
            FileOutputStream outs = new FileOutputStream(objFile);
            byte[] buffer = new byte[1024 * 512];
            int length;
            while ((length = ins.read(buffer)) != -1) {
                outs.write(buffer, 0, length);
            }
            ins.close();
            outs.flush();
            outs.close();
        } else {
            String objFolder = objFolderFile.getPath() + File.separator + resFile.getName();
            File _objFolderFile = new File(objFolder);
            _objFolderFile.mkdirs();
            for (File sf : resFile.listFiles()) {
                copy(sf, new File(objFolder));
            }
        }
    }


    //以文件流的方式复制文件
    public static void copyFile(String src, String dest) throws IOException {
        FileUtil.copyFile(src, dest);
    }


    /**
     * 将文件（夹）移动到令一个文件夹
     *
     * @param resFile       源文件（夹）
     * @param objFolderFile 目标文件夹
     * @throws java.io.IOException 异常时抛出
     */
    public static void move(File resFile, File objFolderFile) throws IOException {
        copy(resFile, objFolderFile);
        //delete(resFile);

        //开始更名
        //objFolderFile.renameTo(new File(newFileName));
    }


    public static List<String> getFiles(String path){
        List<String> fileNames = new ArrayList<>();
        File file = new File(path);
        File[] array = file.listFiles();

        for(int i=0;i<array.length;i++){
            fileNames.add(array[i].getName());
        }
        return fileNames;
    }


    public static void main(String args[]) throws IOException {
        List<String> list = new ArrayList<>();
        list = getFiles("D:\\word_search_log");
        for(String fname :list){
            fname = fname.substring(0,fname.lastIndexOf("."));
            String dateStr = fname.substring(0,10);
            String type = fname.substring(11,12);
            System.out.println(dateStr+"######"+type);


        }
        /*String fileName = "ok.jpg";

        //上传文件
        String newFileName = System.currentTimeMillis() + RandomCodeGenerator.randomNumberAndLetter(10)+".jpg";
        FileToolkit.copyFile(FileToolkit.OLDPATH + File.separatorChar + fileName, FileToolkit.PICPATH + File.separatorChar + newFileName);
        System.out.println( FileToolkit.PICPATH + File.separatorChar + newFileName);
*/
    }
}